﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelElectCalcModernUI.Service
{
    public class SaveLoadData
    {
        public static void SaveOtherSettingData<T>(T obj)
        {
            SaveSetting<T>(obj, "SettingData");
        }
        public static T LoadOtherSettingData<T>() where T : new()
        {
            return LoadSetting<T>("SettingData");
        }
        public static void SaveCalcData<T>(T obj)
        {
            SaveSetting<T>(obj, "CalcData");
        }
        public static T LoadCalcData<T>() where T : new()
        {
            return LoadSetting<T>("CalcData");
        }
        public static void SaveSkinData<T>(T obj)
        {
            SaveSetting<T>(obj, "SkinData");
        }
        public static T LoadSkinData<T>() where T : new()
        {
            return LoadSetting<T>("SkinData");
        }


        private static T LoadSetting<T>(string settingName) where T : new()
        {
            return LoadSettingFromFile<T>(settingName);
        }
        private static T LoadSettingFromApp<T>(string settingName) where T : new()
        {
            try
            {
                T t = JsonConvert.DeserializeObject<T>(Properties.Settings.Default[settingName].ToString());
                if (t == null)
                    t = new T();
                return t;
            }
            catch
            {
                return new T();
            }
        }

        private static T LoadSettingFromFile<T>(string settingName) where T : new()
        {
            try
            {
                string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Properties.Settings.Default.SettingFile);
                if (!System.IO.File.Exists(path))
                    return new T();
                JObject settingJson = JObject.Parse(System.IO.File.ReadAllText(path));
                JToken jtk = null;
                if (!settingJson.TryGetValue(settingName, out jtk))
                    return new T();
                string child = settingJson.GetValue(settingName).ToString();
                T t = JsonConvert.DeserializeObject<T>(child);
                if (t == null)
                    t = new T();
                return t;
            }
            catch
            {
                return new T();
            }
        }

        private static void SaveSetting<T>(T obj, string settingName)
        {
            SaveSettingToFile<T>(obj, settingName);
        }
        private static void SaveSettingToApp<T>(T obj, string settingName)
        {
            string json = JsonConvert.SerializeObject(obj);
            Properties.Settings.Default[settingName] = json;
            Properties.Settings.Default.Save();
        }

        private static void SaveSettingToFile<T>(T obj, string settingName)
        {
            try
            {
                string json = JsonConvert.SerializeObject(obj);
                string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Properties.Settings.Default.SettingFile);
                JObject settingJson = null;
                if (!System.IO.File.Exists(path))
                { 
                    settingJson = JObject.Parse("{}");
                }
                else
                {
                    try
                    {
                        settingJson = JObject.Parse(System.IO.File.ReadAllText(path));
                    }
                    catch
                    {
                        settingJson = JObject.Parse("{}");
                    }
                }
                JToken jtk = null;
                if (!settingJson.TryGetValue(settingName, out jtk))
                {
                    settingJson.Add(new JProperty(settingName, json));
                }
                else
                {
                    settingJson.Property(settingName).Value = json;
                }
                using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.Create))
                {
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                    sw.Write(settingJson.ToString());
                    sw.Flush();
                }
            }
            catch
            {
            }
        }
    }
}
